# http://www.vtk.org/Wiki/CMakeMacroLibtoolFile
#
# This macro creates a libtool .la file for shared libraries or plugins. The
# first parameter is the target name of the library, the second parameter is
# the directory where it will be installed to. For example, for a KDE 3.x
# module named "kfoo", the usage would be as follows:
#
# ADD_LIBRARY(foo SHARED kfoo1.cpp kfoo2.cpp)
# CREATE_LIBTOOL_file(foo /lib/kde3)
#
# The macro get_target_property_with_default is helpful to handle properties
# with default values.

if(DEFINED __INCLUDED_CREATE_LIBTOOL_FILE)
    return()
endif()
set(__INCLUDED_CREATE_LIBTOOL_FILE TRUE)

MACRO(get_target_property_with_default _variable _target _property _default_value)
    get_target_property (${_variable} ${_target} ${_property})
    IF (${_variable} MATCHES NOTFOUND)
     SET (${_variable} ${_default_value})
    ENDIF (${_variable} MATCHES NOTFOUND)
ENDMACRO (get_target_property_with_default)

MACRO(CREATE_LIBTOOL_FILE _target _install_DIR)
    get_target_property(_target_name ${_target} OUTPUT_NAME)
    get_target_property_with_default(_target_static_lib ${_target} STATIC_LIB "")
    get_target_property_with_default(_target_dependency_libs ${_target} LT_DEPENDENCY_LIBS "")
    get_target_property_with_default(_target_current ${_target} LT_VERSION_CURRENT 0)
    get_target_property_with_default(_target_age ${_target} LT_VERSION_AGE 0)
    get_target_property_with_default(_target_revision ${_target} LT_VERSION_REVISION 0)
    get_target_property_with_default(_target_installed ${_target} LT_INSTALLED yes)
    get_target_property_with_default(_target_shouldnotlink ${_target} LT_SHOULDNOTLINK yes)
    get_target_property_with_default(_target_dlopen ${_target} LT_DLOPEN "")
    get_target_property_with_default(_target_dlpreopen ${_target} LT_DLPREOPEN "")
    set(_soname lib${_target_name}.so)
    set(_laname ${CMAKE_CURRENT_BINARY_DIR}/lib${_target_name}.la)
    file(WRITE ${_laname} "# ${_laname} - a libtool library file\n")
    file(APPEND ${_laname} "# Generated by CMake ${CMAKE_VERSION} (like GNU libtool)\n")
    file(APPEND ${_laname} "\n# Please DO NOT delete this file!\n# It is necessary for linking the library with libtool.\n\n" )
    file(APPEND ${_laname} "# The name that we can dlopen(3).\n")
    file(APPEND ${_laname} "dlname='${_soname}'\n\n")
    file(APPEND ${_laname} "# Names of this library.\n")
    file(APPEND ${_laname} "library_names='${_soname}.${_target_current}.${_target_age}.${_target_revision} ${_soname}.${_target_current} ${_soname}'\n\n")
    file(APPEND ${_laname} "# The name of the static archive.\n")
    file(APPEND ${_laname} "old_library='${_target_static_lib}'\n\n")
    file(APPEND ${_laname} "# Libraries that this one depends upon.\n")
    file(APPEND ${_laname} "dependency_libs='${_target_dependency_libs}'\n\n")
    file(APPEND ${_laname} "# Names of additional weak libraries provided by this library\n")
    file(APPEND ${_laname} "weak_library_names=\n\n")
    file(APPEND ${_laname} "# Version information for ${_laname}.\n")
    file(APPEND ${_laname} "current=${_target_current}\n")
    file(APPEND ${_laname} "age=${_target_age}\n")
    file(APPEND ${_laname} "revision=${_target_revision}\n\n")
    file(APPEND ${_laname} "# Is this an already installed library?\n")
    file(APPEND ${_laname} "installed=${_target_installed}\n\n")
    file(APPEND ${_laname} "# Should we warn about portability when linking against -modules?\n")
    file(APPEND ${_laname} "shouldnotlink=${_target_shouldnotlink}\n\n")
    file(APPEND ${_laname} "# Files to dlopen/dlpreopen\n")
    file(APPEND ${_laname} "dlopen='${_target_dlopen}'\n")
    file(APPEND ${_laname} "dlpreopen='${_target_dlpreopen}'\n\n")
    file(APPEND ${_laname} "# Directory that this library needs to be installed in:\n")
    file(APPEND ${_laname} "libdir='${CMAKE_INSTALL_PREFIX}${_install_DIR}'\n")
    INSTALL( FILES ${_laname} DESTINATION ${CMAKE_INSTALL_PREFIX}${_install_DIR})
ENDMACRO(CREATE_LIBTOOL_FILE)
